import React from "react";
import { Bi, Ei } from "@/module-00-codebase/pkg-01-container";
import { Throwable } from "@/module-00-codebase/pkg-02-tool";
import { Field } from "@/module-00-codebase/pkg-08-entity/field-entity";
import BaseDataField from "./base/BaseDataField";

export class DataField extends BaseDataField {
  public async elmtInit() {
    this.RealDataField = findRealDataField(this.getField());
  }
  public elmtView(): JSX.Element | null {
    return <this.RealDataField params={[this.getField(), this.getData()]} init={impl => Bi.utils.prxyImpl(this, impl)} />;
  }
  private RealDataField = BaseDataField;
}

const findRealDataField = (field: Field): typeof DataField => {
  let Elmt: typeof DataField;
  /*--*/ if (!!(Elmt = (Ei as any)[`${PREFIX}${field.declaringType}_${field.name}`])) {
  } else if (field.izArray && !!(Elmt = (Ei as any).AttrArry)) {
  } else if (!!field.orefType && !!(Elmt = (Ei as any)[`${PREFIX}${field.orefType}`])) {
  } else if (!!(Elmt = (Ei as any)[`${PREFIX}${Bi.utils.upperFirst(field.type)}`])) {
  } else return Throwable(`${PREFIX}${Bi.utils.upperFirst(field.type)} doesn't exist.`);
  return Elmt;
};
const PREFIX = "DataField";
